package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.LayoutNeedCompanylistBinding;
import com.sgb.kjwl.databinding.LayoutNeedRequirementBinding;
import com.sgb.kjwl.databinding.LayoutNeedXiajiBinding;
import com.sgb.kjwl.databinding.LayoutNeedmessageBinding;
import com.sgb.kjwl.databinding.LayoutRecommendBinding;
import com.sgb.kjwl.model.entity.NeedDetailsEntity;
import com.sgb.kjwl.model.entity.enterprise.ListBean;
import com.sgb.kjwl.model.entity.enterprise.NeedDetailSignUpEntity;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.enterprise.NeedSignUpListActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.NewNeedDetailsActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.SignUpDetailActivity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.SharedPreferenceUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * 需求详情适配
 */
public class NewNeedDataAdpater extends RecyclerView.Adapter<ItemViewHolder> {
    private Context mContext;
    private NeedDetailsEntity needDetail;
    private List<ListBean> list_data = new ArrayList<>();
    private boolean showrecomend = false;//是否展示推荐
    private boolean showxiajia = true;
    private boolean showmore = false;
    private boolean ishome = false;//是否是从前台页面进入的详情
    private List<NeedDetailSignUpEntity> list_signup = new ArrayList<>();

    public NewNeedDataAdpater(Context mContext) {
        this.mContext = mContext;
    }

    public void setNeedDetail(NeedDetailsEntity needDetail) {
        this.needDetail = needDetail;
        notifyDataSetChanged();
    }

    public void setList_data(List<ListBean> list_data) {
        this.list_data = list_data;
        notifyDataSetChanged();
    }

    public void setRecomendisShow(boolean type_showrecomend) {//是否展示是推荐
        this.showrecomend = type_showrecomend;
        notifyDataSetChanged();
    }

    public void setShowxiajia(boolean xiajia) {
        this.showxiajia = xiajia;
        notifyDataSetChanged();
    }


    public void setIshome(boolean ishome){
        this.ishome = ishome;

    }


    public void setNeedDetailSignUpData(List<NeedDetailSignUpEntity> listdata) {
        list_signup.clear();
        if (listdata.size() > 3) {
            showmore = true;
            for (NeedDetailSignUpEntity listdatum : listdata) {
                list_signup.add(listdatum);
                if (list_signup.size() >= 3) {
                    break;
                }
            }
        } else {
            list_signup.addAll(listdata);
        }
        notifyDataSetChanged();
    }


    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @NonNull
    @NotNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        Log.e("viewtype", viewType + "");
        if (needDetail != null) {
            if ("1".equals(needDetail.demandStatus)) {
                switch (viewType) {
                    case 0:
                        return ItemViewHolder.get(mContext, parent, R.layout.layout_needmessage);
                    case 1:
                        return ItemViewHolder.get(mContext, parent, R.layout.layout_need_requirement);
                    case 2:
                        return ItemViewHolder.get(mContext, parent, R.layout.layout_need_companylist);
                    case 3://为你推荐
                        return ItemViewHolder.get(mContext, parent, R.layout.layout_recommend);
                }
            } else if (showxiajia) {
                switch (viewType) {
                    case 0:
                        return ItemViewHolder.get(mContext, parent, R.layout.layout_need_xiaji);
                    case 1:
                        return ItemViewHolder.get(mContext, parent, R.layout.layout_recommend);
                }
            } else {
                switch (viewType) {
                    case 0:
                        return ItemViewHolder.get(mContext, parent, R.layout.layout_needmessage);
                    case 1:
                        return ItemViewHolder.get(mContext, parent, R.layout.layout_need_requirement);
                    case 2:
                        return ItemViewHolder.get(mContext, parent, R.layout.layout_need_companylist);
                    case 3://为你推荐
                        return ItemViewHolder.get(mContext, parent, R.layout.layout_recommend);
                }
            }
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ItemViewHolder holder, int position) {

        if (holder.getmBinding() instanceof LayoutNeedmessageBinding) {//需求信息

            if (needDetail != null) {
                holder.getItem().setVisibility("1".equals(needDetail.demandStatus) ? View.VISIBLE : showxiajia ? View.GONE : View.VISIBLE);
                holder.setBinding(BR.model, needDetail);
                if (!TextUtils.isEmpty(needDetail.getDemandAttachment())&&!needDetail.getDemandAttachment().equals("{}")) {
                    ((LayoutNeedmessageBinding) holder.getmBinding()).linCode.setVisibility(View.VISIBLE);
                    if (needDetail.getDemandAttachment().indexOf("{") > -1) {

                        JSONObject jsonObject = JSONObject.parseObject(needDetail.getDemandAttachment());
                        if (null != jsonObject.get("name")) {
                            String name = jsonObject.getString("name");
                            ((LayoutNeedmessageBinding) holder.getmBinding()).tvName.setText(name);
                        }
                        if (null != jsonObject.get("size")) {
                            ((LayoutNeedmessageBinding) holder.getmBinding()).tvSize.setText(jsonObject.getString("size"));
                        }
                        if (jsonObject.get("type") != null) {
                            String fileType = jsonObject.getString("type");

                            switch (fileType) {
                                case "application/zip":
                                case "application/rar":
                                    Glide.with(mContext).load(R.mipmap.icon_zip).into(((LayoutNeedmessageBinding) holder.getmBinding()).image);
                                    break;
                                case "image/png":
                                case "image/jpeg":
                                    Glide.with(mContext).load(R.mipmap.icon_image).into(((LayoutNeedmessageBinding) holder.getmBinding()).image);
                                    break;
                                case "application/pdf":
                                    Glide.with(mContext).load(R.mipmap.icon_pdf2).into(((LayoutNeedmessageBinding) holder.getmBinding()).image);
                                    break;

                            }
                        }

                    }
                } else {
                    ((LayoutNeedmessageBinding) holder.getmBinding()).linCode.setVisibility(View.GONE);
                }

            }
        }

        if (holder.getmBinding() instanceof LayoutNeedRequirementBinding) {//报价要求
            if (needDetail != null) {
                holder.getItem().setVisibility("1".equals(needDetail.demandStatus) ? View.VISIBLE : showxiajia ? View.GONE : View.VISIBLE);
                ((LayoutNeedRequirementBinding) holder.getmBinding()).tvPiao.setText(null==needDetail.getInvoiceRequirements()?"/":needDetail.getInvoiceRequirements() == 0 ? "不需要发票" : needDetail.getInvoiceRequirements() == 1 ? "增值税普通发票" : needDetail.getInvoiceRequirements() == 2 ? "增值税专用发票" : "/");

                String path = "/";
                if (!TextUtils.isEmpty(needDetail.getProvinceName())) {
                    path = needDetail.getProvinceName();
                }
                if (!TextUtils.isEmpty(needDetail.getCityName())) {
                    path = path + needDetail.getCityName();
                }
                if (!TextUtils.isEmpty(needDetail.getAreaName())) {
                    path = path + needDetail.getAreaName();
                }


                ((LayoutNeedRequirementBinding) holder.getmBinding()).tvPath.setText(path.toString());

                if (needDetail.requirementTypes == 5) {
                    ((LayoutNeedRequirementBinding) holder.getmBinding()).viewLin2.setVisibility(View.VISIBLE);
                    ((LayoutNeedRequirementBinding) holder.getmBinding()).linService.setVisibility(View.VISIBLE);
                    String serice_zt = "/";
                    if (needDetail.servicePrincipal.indexOf("1") > -1 && needDetail.servicePrincipal.indexOf("2") > -1) {
                        serice_zt = "企业、团队";
                    } else if (needDetail.servicePrincipal.indexOf("2") > -1) {
                        serice_zt = "团队";
                    } else if (needDetail.servicePrincipal.indexOf("1") > -1) {
                        serice_zt = "企业";
                    }
                    ((LayoutNeedRequirementBinding) holder.getmBinding()).tvServicetype.setText(serice_zt);
                } else {
                    ((LayoutNeedRequirementBinding) holder.getmBinding()).viewLin2.setVisibility(View.GONE);
                    ((LayoutNeedRequirementBinding) holder.getmBinding()).linService.setVisibility(View.GONE);
                }
            }
        }

        if (holder.getmBinding() instanceof LayoutNeedCompanylistBinding) {//报价单位
            if (needDetail != null) {
                holder.getItem().setVisibility("1".equals(needDetail.demandStatus) ? View.VISIBLE : showxiajia ? View.GONE : View.VISIBLE);


                if (list_signup != null && list_signup.size() > 0) {

                    ((LayoutNeedCompanylistBinding) holder.getmBinding()).llNulllist.setVisibility(View.GONE);
                    ((LayoutNeedCompanylistBinding) holder.getmBinding()).recylerview.setVisibility(View.VISIBLE);
                    ((LayoutNeedCompanylistBinding) holder.getmBinding()).tvLookmore.setVisibility(showmore?View.VISIBLE:View.GONE);
                    ((LayoutNeedCompanylistBinding) holder.getmBinding()).viewLine.setVisibility(showmore?View.VISIBLE:View.GONE);
                    NeedDetailSignuoAdpater adpater = new NeedDetailSignuoAdpater(mContext);
                    adpater.setList(false);
                    adpater.setCheckuser(ismeno(needDetail.publishType,needDetail.getCompanyNo(),needDetail.getTeamNo(),needDetail.getUserNo()));
                    for (int i = list_signup.size() - 1; i >= 0; i--) {
                        if(i>2){
                            list_signup.remove(i);
                        }
                    }
                    adpater.setdata(list_signup);
                    ((LayoutNeedCompanylistBinding) holder.getmBinding()).recylerview.setLayoutManager(new LinearLayoutManager(mContext));
                    ((LayoutNeedCompanylistBinding) holder.getmBinding()).recylerview.setAdapter(adpater);


                    adpater.setOnItemClick(new AdapterOnItemClick<NeedDetailSignUpEntity>() {
                        @Override
                        public void onItemClick(NeedDetailSignUpEntity needDetailSignUpEntity, int position) {
                            if(ismeno(needDetail.publishType,needDetail.getCompanyNo(),needDetail.getTeamNo(),needDetail.getUserNo())/*&&needDetail.getOfferStatus()==1*/&&!ishome) {
                                SignUpDetailActivity.start(mContext, needDetailSignUpEntity, needDetail);//报价详情
                            }
                        }
                    });


                    ((LayoutNeedCompanylistBinding)holder.getmBinding()).tvLookmore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            NeedSignUpListActivity.start(mContext,needDetail.id+"",ishome);
                        }
                    });

                }

            }
        }

        if (holder.getmBinding() instanceof LayoutNeedXiajiBinding) {//下架
            if (needDetail != null) {
                holder.getItem().setVisibility("1".equals(needDetail.demandStatus) ? View.GONE : showxiajia ? View.VISIBLE : View.GONE);
            }
        }

        if (holder.getmBinding() instanceof LayoutRecommendBinding) {//推荐
            if (list_data.size() > 0) {
                holder.getItem().setVisibility(View.VISIBLE);
                ((LayoutRecommendBinding) holder.getmBinding()).rvRecommendForYou.setLayoutManager(new LinearLayoutManager(mContext));
                NeedNewAdapter madpater = new NeedNewAdapter(mContext, list_data);
                ((LayoutRecommendBinding) holder.getmBinding()).rvRecommendForYou.setAdapter(madpater);
                madpater.setOnItemClick(new AdapterOnItemClick<ListBean>() {
                    @Override
                    public void onItemClick(ListBean listBean, int position) {
                        NewNeedDetailsActivity.startactivity(mContext, listBean.id + "");
                    }
                });
            }

        }


    }

    /**
     * 判定是否是自己账号
     */
    public boolean ismeno(int publishtype,String companyno,String teamno,String userno){
        int metype = UserOperating.getInstance().isCompany()?1:UserOperating.getInstance().isTeam()?2:0;
        switch (publishtype){
            case 1://公司
                if(metype==1&&SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO,"").equals(companyno)){
                    return true;
                }
                break;
            case 2://团队
                if(metype==2&&SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO,"").equals(teamno)){
                    return true;
                }
                break;
            default:
                if(metype==0&&SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO,"").equals(userno)){
                    return true;
                }
                break;
        }

        return false;
    }


    @Override
    public int getItemCount() {
        if (needDetail != null) {
            if ("1".equals(needDetail.demandStatus)) {//上架状态
                return 3 + (showrecomend ? 1 : 0);
            } else if (showxiajia) {
                return 1 + (showrecomend ? 1 : 0);
            } else {
                return 3 + (showrecomend ? 1 : 0);
            }
        }

        return 0;

    }
}
